home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / comm / tcp / rxsocket.lha / rxsocket / examples / revserv.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1998-10-27  |  1.9 KB  |  87 lines

  1. /*
  2.     A simple stand alone service example.
  3.     It waits for connections on port 4000.
  4.     After a connection is made,
  5.     it waits for a string of max 256 char len and send it back reversed.
  6.  
  7.     To run it:
  8.     - be sure TCP port 4000 is free (e.g. you don't have pserv.rexx
  9.       enabled in inetd database, if you installed it).
  10.       Open a shell and write:
  11.       - rx revserv.
  12.  
  13.     To stop it:
  14.     - CTRL_C in the shell where rvserv is running
  15.     or
  16.     - rx "shell 'REVSERVPORT' 'QUIT'"
  17. */
  18.  
  19. prg = ProgramName("NOEXT")
  20.  
  21. /**START libraries*/
  22. if ~show("L","rexxsupport.library") then
  23.     if ~addlib("rexxsupport.library",0,-30) then call err "no rexxsupport.library"
  24.  
  25. if ~show("L","rxsocket.library") then
  26.     if ~addlib("rxsocket.library",0,-30) then call err "no rxsocket.library"
  27.  
  28. if ~show("L","rmh.library") then
  29.     if ~addlib("rmh.library",0,-30) then call err "no rmh.library"
  30. /**END libraries**/
  31.  
  32. if ~Open("STDERR","*","W") | ~OpenPort("REVSERVPORT") then exit
  33.  
  34. sock = socket("INET","STREAM","IP")
  35. if sock<0 then call err "can't create socket:" errno()
  36.  
  37. local.ADDRFAMILY = "INET"
  38. local.ADDRADDR    = 0
  39. local.ADDRPORT   = 4000
  40. if bind(sock,"LOCAL")<0 then call err "can't bind port 4000:" errno()
  41.  
  42. sig = PortSignal("REVSERVPORT")
  43. SEL.READ.0=sock
  44. open = 1
  45. do while open
  46.  
  47.     if Listen(sock,5)<0 then call err "listen error:" errno()
  48.  
  49.     res = WaitSelect("SEL",,,sig)
  50.  
  51.     pkt = GetPkt("REVSERVPORT")
  52.     if pkt ~= null() then do
  53.         comm= GetArg(pkt)
  54.         call reply(pkt)
  55.         if upper(comm) == "QUIT" then open = 0
  56.     end
  57.  
  58.     if sel.0.read then do
  59.  
  60.         lsock = accept(sock,"REMOTE")
  61.         if lsock<0 then call err "accept() error:" errno()
  62.  
  63.         auth = AuthFun(remote.addrAddr,4000,remote.addrPort)
  64.         say auth
  65.  
  66.         len = recv(lsock,"BUF",256)
  67.         if len>0 then do
  68.             buf = reverse(buf)
  69.             if send(lsock,buf) ~= length(buf) then call err "send() error:" errno()
  70.         end
  71.         if ( len < 0 ) & ( errno() ~= 35 )
  72.             then call err "recv() error:" errno()
  73.  
  74.         call CloseSocket(lsock)
  75.  
  76.     end
  77.  
  78. end
  79.  
  80. exit
  81.  
  82. err: procedure expose prg
  83. parse arg msg
  84.     say prg":" msg
  85.     exit
  86.  
  87.